package org.soderiakelvy.model.dao.imp;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.soderiakelvy.model.dao.VehiculoDao;
import org.soderiakelvy.model.domain.VehiculoVO;
import org.soderiakelvy.model.util.HibernateUtil;

public class VehiculoDaoImp implements VehiculoDao, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void insertVehiculoVO(VehiculoVO vehiculo) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(vehiculo);
        session.getTransaction().commit();
        session.close();

	}

	@Override
	public void updateVehiculoVO(VehiculoVO vehiculo) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.update(vehiculo);
        session.getTransaction().commit();
        session.close();

	}

	@Override
	public void deleteVehiculoVO(VehiculoVO vehiculo) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.delete(vehiculo);
        session.getTransaction().commit();
        session.close();

	}

	@Override
	public List<VehiculoVO> getAllVehiculo() {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(VehiculoVO.class);
        List<VehiculoVO> result = criteria.list();
        session.close();
        return result;
	}

	@Override
	public List<VehiculoVO> getVehiculoByEstado(String estado) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(VehiculoVO.class);
        criteria.add(Restrictions.eq("estado", estado));
        List<VehiculoVO> result = criteria.list();
        session.close();
        return result;
	}

	@Override
	public VehiculoVO getVehiculoById(long vehiculoId) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(VehiculoVO.class);
		criteria.add(Restrictions.eq("vehiculoId", vehiculoId));
		VehiculoVO result = (VehiculoVO) criteria.uniqueResult();
		session.close();
		return result;
	}

}
